Assembly Language
©
Copyright Brian Brown, 1988-2000. All rights reserved.
| Notes | Home Page |
ASSEMBLY LANGUAGE PROGRAMMING, Part 7
INTERFACING TO HLL ROUTINES
There are times that high level
languages need to call assembly language modules. This results due to
constraints like speed and memory space.
We shall look at interfacing a Pascal program to an assembly language module.
The Pascal program will declare an integer based array, and pass the address of this array, and the number of elements in the array, to an assembly language module.
Using the address, the assembly language module will add the sum of the array, returning the result to the Pascal program.
The assembly language module is shown below.
TITLE Addup88 .MODEL TPASCAL .CODE PUBLIC Addup Addup Proc Far Array : DWORD, Elements : WORD RETURNS Reslt : WORD push ds ; save ds register push cx ; save cx register push si ; save si register lds si, Array ; point DS:SI to array element1 mov cx, Elements ; count of elements xor ax, ax ; clear total lp1: add ax, [si] ; add value to total inc si ; next element inc si dec cx jne lp1 pop si pop cx pop ds RET ; exit to Pascal Module with ; result in AX Addup ENDP END
This is compiled to OBJECT code by the command
$TASM ADDUP88
The Pascal module is shown below.
Program ADDDEMO (input, output); Uses DOS, CRT; Type IntArray = Array[1..20] of Integer; Var Numbers : IntArray; Result : Integer; Loop : Integer; {$F+} Function Addup( var Numbers : IntArray; Elements : Integer ) : Integer ; EXTERNAL; {$L ADDUP88.OBJ} {$F-} begin for loop:= 1 to 20 do Numbers[loop] := loop; Result := Addup( Numbers, 20 ); Writeln('The sum of the array is ', Result) End.
When compiled under Turbo Pascal, the two object modules are linked together, creating an executable file.
ASSEMBLER OPTIONS
Various options are supported by most assemblers.
These options provide for
COMMAND FILES
Command files are text files which contain commands
to the assembler.
$TASM @MYCMDFIL
will invoke the assembler using the options specified in the file Mycdfil. If this file contained the following,
/a /e myprog, myobj, mylst;
this is equivalent to typing
$TASM /a /e myprog, myobj, mylst;
This simplifies the process of having to repeat all the command line options whilst the program is being debugged.
CONDITIONAL ASSEMBLY OF SOURCE CODE STATEMENTS
The following
directives are used to specify to the assembler, whether or not to assemble the
bracketed group of statements which follow.
IF ELSE ENDIF IFDEF IFNDEF
The IF directives and the ENDIF and ELSE directives can be used to enclose the statements to be considered for conditional assembly.
The conditional block of statements is used as follows,
IF debug xor ax,ax ELSE xor bx,bx ENDIF
If the symbol debug equates to true (non-zero), the ax register will be cleared, otherwise the bx register will be cleared.
The IFDEF and IFNDEF directives test whether or not the given variable name/symbol has been defined.
IFDEF buffer buf1 DB 10 DUP(?) ENDIF
In this example, buf1 is allocated only if buffer has previously been defined. It consists of ten bytes whose initial value is undefined.
THE INCLUSION OF SOURCE MACROS AND DEFINITIONS
A macro or
definition file is a collection of definitions or program code which can be
included into the source code program. A macro file is simply a file containing
macro definitions.
The programmer adds these definitions to the source file using the include directive, and may remove unwanted definitions using the purge directive.
The include directive inserts the definitions or code statements from the specified file into the current source file during assembly, and allows any variables or declarations in the include file to be referenced or accessed in the source program being written.
INCLUDE entry INCLUDE b:\include\c_stuff
LIST FILES
List files have already been covered under section 6
dealing with CRS8.
The format for invoking the 8088 assembler is,
TASM sourceasmfile, objfilename, listfilename
or the /l option can be specified on the command line.
The following 8088 assembler directives can disable and enable the output listing.
%NOLIST %LIST
Consider the following 8088 assembly language program.
TITLE Doscall ;Doscall.asm source file .MODEL SMALL CR equ 0ah LF equ 0dh EOSTR equ '$' .stack 200h .data message db 'Hello and welcome.' db CR, LF, EOSTR .code print proc near mov ah,9h ;PCDOS print function int 21h ret print endp start: mov ax, @data mov ds, ax mov dx, offset message call print mov ax, 4c00h int 21h end start
When assembled with the following command line options,
$TASM /l /n Doscall;
It generates a listing file. The list file for the program looks like,
Turbo Assembler Version 1.0 21-05-89 13:27:31 Page 1 DOSCALL.ASM Doscall 1 0000 .MODEL SMALL 2 3 = 000A CR equ 0ah 4 = 000D LF equ 0dh 5 = 0024 EOSTR equ '$' 6 7 0000 .stack 200h 8 9 0000 .data 10 0000 48 65 6C 6C 6F 20 61 + message db 'Hello and welcome.' 11 6E 64 20 77 65 6C 63 + 12 6F 6D 65 2E 13 0012 0A 0D 24 db CR, LF, EOSTR 14 15 0015 .code 16 0000 print proc near 17 0000 B4 09 mov ah,9h ;PCDOS print function 18 0002 CD 21 int 21h 19 0004 C3 ret 20 0005 print endp 21 22 0005 B8 0000s start: mov ax, @data 23 0008 8E D8 mov ds, ax 24 000A BA 0000r mov dx, offset message 25 000D E8 FFF0 call print 26 0010 B8 4C00 mov ax, 4c00h 27 0013 CD 21 int 21h 28 29 end start
The s on line 22 indicates a segment register value which is filled in by the DOS loader when the program is loaded into memory. The r on line 24 indicates a relative value which is also filled in by the DOS loader.
SYMBOLIC INFORMATION
Symbolic information is useful in determining
the size and location of variables, segments etc. This information is used when
debugging the program or locating the program in Eprom.
The 8088 assembler options available are,
/c cross-reference in list file /l listfile generated /n suppress symbol table in list file /zd line numbers in object code /zi debug info in object code for debugger
When the previous program Doscall.asm is assembled with a list file and symbol plus cross-referencing, the additional information appended to the list file is,
Turbo Assembler Version 1.0 21-05-89 13:35:03 Page 2 Symbol Table Symbol Name Type Value Cref defined at # ??DATE Text "21-05-89" ??FILENAME Text "DOSCALL " ??TIME Text "13:35:03" ??VERSION Number 0100 @CODE Text _TEXT #1 #15 @CODESIZE Text 0 #1 @CPU Text 0101H @CURSEG Text _TEXT #9 #15 @DATA Text DGROUP #1 22 @DATASIZE Text 0 #1 @FILENAME Text DOSCALL @WORDSIZE Text 2 #9 #15 CR Number 000A #3 13 EOSTR Number 0024 #5 13 LF Number 000D #4 13 MESSAGE Byte DGROUP:0000 #10 24 PRINT Near _TEXT:0000 #16 25 START Near _TEXT:0005 #22 29 Groups & Segments Bit Size Align Combine Class Cref defined at # DGROUP Group #1 1 22 STACK 16 0200 Para Stack STACK #7 _DATA 16 0015 Word Public DATA #1 #9 _TEXT 16 0015 Word Public CODE #1 1 #15 15
This also shows which lines variables and labels were defined and referenced.